// routes/idcard.js

const Joi = require("joi");
const Config = require("../config");
const _request = require("../utils/request");

const GROUP_NAME = "idcard";

module.exports = [
    {
        method: "POST",
        path: `/${GROUP_NAME}/getCardInfo`,
        handler: async (request, reply) => {
            let type = request.payload.rtype;
            let result = await _request.get('http://apis.juhe.cn/idcard/index',{
                'cardno': request.payload.no,
                'dtype': type && type !== '' ? type : 'json',
                'key': Config.jhIdCardKey,
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "获取身份证信息",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                payload: {
                    no: Joi.string().required(),
                    rtype: Joi.string(),
                }
            }
        }
    }
];
